package com.wanli.agent.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.BaseResponseBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.mine.UserAgreementActivity;
import com.wanli.agent.mine.model.MineModelImpl;
import com.wanli.agent.utils.CashierInputFilter;
import com.wanli.agent.utils.FastClickUtils;
import com.wanli.agent.utils.NumberFormatUtils;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawNewActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_withdraw_money_1)
    TextView tvWithdrawMoney1;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.cb_remember)
    CheckBox cbRemember;
    @BindView(R.id.tv_withdraw_agree)
    TextView tvWithdrawAgree;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IHomePageModel homePageModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_new);
        ButterKnife.bind(this);
        initView();
        requestUserInfo();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        InputFilter[] filters = {new CashierInputFilter()};
        editMoney.setFilters(filters);
        titleBar.setRightClickListener(() -> {
            showActivity(WithdrawListActivity.class);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestUserInfo();
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                String can_withdrawal_money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getCan_withdrawal_money()) / 100);
                tvWithdrawMoney1.setText(can_withdrawal_money);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    public void withdraw1(String money) {
        ViewLoading.showProgress(mActivity);
        homePageModel.requestAgentWithdrawalAdd(money, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                showActivity(WithdrawListActivity.class);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_withdraw_agree, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_withdraw_agree:
                Intent intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.tv_commit:
                String money = editMoney.getText().toString();
                if (TextUtils.isEmpty(money)) {
                    ToastUtil.showShort("请输入提现金额");
                    return;
                }
                if (!cbRemember.isChecked()) {
                    ToastUtil.showShort("请同意提现协议");
                    return;
                }

                if (FastClickUtils.isFastClick()) {
                    return;
                }
                //钱数 单位分
                String moneyUnit = String.valueOf(Double.parseDouble(money) * 100);
                moneyUnit = moneyUnit.substring(0, moneyUnit.indexOf("."));
                withdraw1(moneyUnit);
                break;
        }
    }
}
